﻿using System;
using System.Windows.Forms;
using Harvest.Api;
using Harvest.Api.Model;

namespace Harvest.WinForms
{

    public partial class SettingsForm : Form
    {

        private Repository _repository;


        public SettingsForm()
        {
            InitializeComponent();

            this.Load += new EventHandler(Form1_Load);
            mainTabControl.TabIndexChanged += new EventHandler(mainTabControl_TabIndexChanged);

            clientsListView.SelectedIndexChanged += new EventHandler(clientsListView_SelectedIndexChanged);
            refreshClientsButton.Click += new EventHandler(refreshClientsButton_Click);
            deleteClientButton.Click += new EventHandler(deleteClientButton_Click);
            addClientButon.Click += new EventHandler(addClientButon_Click);
            editClientButton.Click += new EventHandler(editClientButton_Click);
            deactivateClientButton.Click += new EventHandler(deactivateClientButton_Click);
        }


        void deactivateClientButton_Click(object sender, EventArgs e)
        {
            var entity = (Client)clientsListView.SelectedItems[0].Tag;
            _repository.Clients.Deactivate(entity);
            RefreshClientsList();
        }


        void editClientButton_Click(object sender, EventArgs e)
        {
            var form = new ClientForm((Client)clientsListView.SelectedItems[0].Tag);
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                var entity = form.Client;
                entity = _repository.Clients.Update(entity);
                RefreshClientsList();
            }
            form.Close();
        }


        void addClientButon_Click(object sender, EventArgs e)
        {
            var form = new ClientForm(null);
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                var entity = form.Client;
                entity = _repository.Clients.Add(entity);
                RefreshClientsList();
            }
            form.Close();
        }


        void deleteClientButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Delete the client '" + clientsListView.SelectedItems[0].SubItems[1].Text + "'?", 
                "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                var entity = (Client)clientsListView.SelectedItems[0].Tag;
                _repository.Clients.Remove(entity);
                RefreshClientsList();
            }
        }


        void clientsListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            editClientButton.Enabled = (clientsListView.SelectedItems.Count != 0);
            deleteClientButton.Enabled = (clientsListView.SelectedItems.Count != 0);
            deactivateClientButton.Enabled = (clientsListView.SelectedItems.Count != 0);
        }


        void refreshClientsButton_Click(object sender, EventArgs e)
        {
            RefreshClientsList();
        }


        void Form1_Load(object sender, EventArgs e)
        {
            var provider = new RestRequestProvider(accountTextBox.Text, loginTextBox.Text, passwordTextBox.Text);
            _repository = new Repository(provider);
        }


        void mainTabControl_TabIndexChanged(object sender, EventArgs e)
        {
            switch (mainTabControl.SelectedTab.Tag.ToString())
            {
                case "clients":
                    RefreshClientsList();
                    break;
            }
        }



        void RefreshClientsList()
        {

            Cursor.Current = Cursors.WaitCursor;

            var clients = _repository.Clients.GetAll();

            clientsListView.Items.Clear();
            foreach (var client in clients)
            {
                var item = new ListViewItem();
                item.Tag = client;
                item.Text = client.Id.Value.ToString();
                item.SubItems.Add(client.Name);
                item.SubItems.Add(client.Active.ToString());
                clientsListView.Items.Add(item);
            }

            Cursor.Current = Cursors.Default;
        }


    }


}
